package com.health.trans.controller;

import com.alibaba.fastjson.JSONObject;
import com.health.trans.common.entity.ApiResponse;
import com.health.trans.job.AbstractJob;
import com.health.trans.pojo.TableInfo;
import com.health.trans.pojo.TableRes;
import com.health.trans.pojo.TransParam;
import com.health.trans.util.DataSourceUtil;
import com.health.trans.util.EntityOperations;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.WordUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.persistence.Table;
import javax.validation.Valid;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;

import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;

/**
 * @author qyh
 */
@Api(tags = "将数据抽取处理")
@RestController
@Validated
@Slf4j
@RequestMapping("job")
public class DataTransformController {


    @PostMapping(value = "/trans/mysqlToSth", consumes = {APPLICATION_JSON_VALUE})
    public ApiResponse<List<TableRes>> mysqlToSth(@Valid @RequestBody(required = false) TransParam transParam) {
        transParam.getTables().forEach(TableInfo::check);
        return ApiResponse.ok(AbstractJob.execute(transParam));
    }

    @GetMapping(value = "/trans/getTransTableNames")
    public Object getTransTableNames() {
        Map<String, Long> gt5 = new ConcurrentHashMap<>();
        Map<String, Long> lte5 = new ConcurrentHashMap<>();
        List<String> strings = DataSourceUtil.tablesMap.get("product");
        AtomicLong gt5Value = new AtomicLong(0);
        AtomicLong lte5Value = new AtomicLong(0);
        //EntityOperations.POJO_CLASSES
        //        .parallelStream()
        //        .filter(i -> strings.contains(WordUtils.uncapitalize(i.getSimpleName())))
        //        .parallel()
        //        .forEach(i -> {
        //            CustomRepository<Object, Serializable> repository = EntityOperations.getRepositoryByClass(i);
        //            if (repository == null) {
        //                return;
        //            }
        //            long count = repository.count();
        //            if (count > 5000000) {
        //                gt5.put(i.getAnnotation(Table.class).name(), count);
        //                gt5Value.getAndAdd(count);
        //            } else if (count > 0) {
        //                lte5.put(i.getAnnotation(Table.class).name(), count);
        //                lte5Value.getAndAdd(count);
        //            }
        //        });
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("大于 500万 " + gt5.size() + ",总数据：" + gt5Value.get(), gt5);
        jsonObject.put("小于 500万 " + lte5.size() + ",总数据：" + lte5Value.get(), lte5);
        jsonObject.put("总数据：", lte5Value.getAndAdd(gt5Value.get()));
        return ApiResponse.ok(jsonObject);
    }

}
